// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import VueResource from 'vue-resource'
import 'common/stylus/index.styl'
import store from './store/index.js'
// import store from './store' // store对象

import FastClick from 'fastclick'
import ElementUI from 'element-ui'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import 'element-ui/lib/theme-chalk/index.css'
// import './common/stylus/element-ui/theme-default/index.css'
import {
    WechatPlugin
} from 'vux'
import tableUtils from './common/js/table_utils' // 表头工具方法
import groupUtils from './common/js/group_utils' // 分组工具方法
import cellUtils from './common/js/cell_utils' // 分组工具方法
import utils from './common/js/utils' // 工具方法
import componentSelf from './common/component/index.js' // 全局自定义组件引入
import uploader from 'vue-simple-uploader' // 分片上传工具

import md5 from '../static/MD5'

// import axios from 'axios'
// Vue.prototype.$http = axios

Vue.config.productionTip = false
Vue.use(WechatPlugin)
Vue.use(VueResource)
Vue.use(ElementUI)
Vue.use(tableUtils)
Vue.use(groupUtils)
Vue.use(cellUtils)
Vue.use(utils)
Vue.use(componentSelf) // element-input对金额输入框
Vue.use(uploader)

Vue.http.options.emulateJSON = false
// Vue.http.options.headers = {
// 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
// }

// Vue.http.headers.common['Authorization'] = 'f9616d71-364f-459c-9009-d1dce25ae8f1'

Vue.http.interceptors.push((request, next) => {
    nprogress.start()
    // 	var tokens = localStorage.getItem('token')
    var tokens = localStorage.AuthorizationToken2
    var timestamp = new Date().getTime()
    timestamp = timestamp + ''
    let signature = ''
    // 区分 娱加云 和 利云 接口
    if (request.url.indexOf(store.state.yjyUrl) >= 0) {
        // 娱加云
        let desKey = 'krixh27rnfis9567wnr83n48'
        let companyCode = store.state.companyCode
        signature = md5(companyCode + timestamp + desKey).toUpperCase()
    } else {
        // 利云
        signature = md5(tokens + timestamp + localStorage.AuthorizationTokenKey).toUpperCase()
    }
    // token只有为空和不存在
    request.headers.set('Authorization', tokens)
    request.headers.set('timestamp', timestamp)
    request.headers.set('loginType', '1')
    request.headers.set('signature', signature)
    console.log(request.headers)
    console.log('this.$store.state.hasLogin')
    console.log(store)
    next((response) => {
        console.log(response)
        if (parseInt(response.body.ret) === 11162) {
            store.dispatch('setHasLogin', false)
        }
        if (parseInt(response.body.ret) === 1005 || parseInt(response.body.ret) === 1006 || parseInt(response.body.ret) === 1000) {
            store.dispatch('setHasLogin', false)
        }
        if (parseInt(response.body.ret) < 0) {
            store.dispatch('setMsg', '操作错误，请联系系统管理员')
            store.dispatch('setShowMsg', !store.state.showMsg)
        }
        if (parseInt(response.body.ret) >= 10000) {
            store.dispatch('setMsg', response.body.msg)
            store.dispatch('setShowMsg', !store.state.showMsg)
        }
        nprogress.done()
        return response
    })
})

// Vue.http.headers.common['Authorization'] = 'f9616d71-364f-459c-9009-d1dce25ae8f1'

// console.log(Vue.wechat) // 可以直接访问 wx 对象。

// vux要求的快速点击
FastClick.attach(document.body)

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    store,
    template: '<App/>',
    components: {
        App
    }
})
